﻿using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Swiss;
using System.Runtime.InteropServices;

namespace TestProject
{
    [TestClass]
    public class LowLevel_Storage
    {

        public LowLevel_Storage()
        {
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void Test_Signature()
        {
            Console.WriteLine(Directory.GetCurrentDirectory());

            //Setup
            //FileStream fs = File.OpenRead(@"C:\Users\User\Documents\Visual Studio 2008\Projects\Libraries\test.imp");
            FileStream fs = File.OpenRead(@"C:\Users\User\Documents\Visual Studio 2008\Projects\Libraries\MSINFO32.CHM");
            //StorageLowLevel sll = new StorageLowLevel();
            StorageLowLevel.StructuredStorageHeader ssh = new StorageLowLevel.StructuredStorageHeader(true);

            Int32 count = ssh._abSig.Length;
            count = ssh.clid.Length;
            count = ssh._sectFat.Length;
            count = Marshal.SizeOf(ssh);

            Assert.AreEqual(count, 512, "StructuredStorageHeader size not 512");
            Assert.IsTrue(fs.Length > 512);
            Assert.IsTrue(fs.Position == 0);

            //Read Bytes.
            byte[] readBuffer = new byte[count];
            BinaryReader reader = new BinaryReader(fs);
            readBuffer = reader.ReadBytes(count);
            //Copy to Struct.
            GCHandle handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned);
            ssh = (StorageLowLevel.StructuredStorageHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(StorageLowLevel.StructuredStorageHeader));
            handle.Free();

            if (ssh.isStorage)
            {
                Console.WriteLine("Storage Signature: {0}", "OK");
                if (ssh.isBeta2)
                {
                    Console.WriteLine("Late '92 Beta 2 Storage: {0}", ssh.isBeta2);
                }

                Console.WriteLine("Format Version: {0}", ssh.FormatVersion);
                Console.WriteLine("Sector Size: {0} Byte(s)", ssh.SectorSize);
                Console.WriteLine("Mini Sector Size: {0} Byte(s)", ssh.MiniSectorSize);
                Console.WriteLine("Intel Byte-ordering: {0}", ssh.IntelByteOrdering);

                Console.WriteLine("Number of Sect in FAT chain: {0}", ssh._csectFat);
                if (ssh._csectFat != 0)
                {
                    Console.WriteLine("First Sect in FAT chain: {0}", ssh._sectDirStart);
                }

                Console.WriteLine("Number of Sect in mini-FAT chain: {0}", ssh._csectMiniFat);
                if (ssh._csectMiniFat != 0)
                {
                    Console.WriteLine("First Sect in mini-FAT chain: {0}", ssh._sectMiniFatStart);
                }

                Console.WriteLine("Number of Sect in DIF chain: {0}", ssh._csectDif);
                if (ssh._csectDif != 0)
                {
                    Console.WriteLine("First Sect in DIF chain: {0}", ssh._sectDifStart);
                }
            }
            else
            {
                Console.WriteLine("Storage Signature: {0}", "Not OK");
            }
            //Assert.AreEqual(512, ssh.SectorSize, "Warning: Sector Size not 512 (default)");
            //Assert.AreEqual(64, ssh.MiniSectorSize, "Warning: Mini-Sector Size not 64 (default)");

            fs.Close();
        }


    }
}
